package com.npjscg.medicalpro.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.npjscg.medicalpro.controller.dto.ChartDTO;
import com.npjscg.medicalpro.controller.dto.HospitalDTO;
import com.npjscg.medicalpro.entity.Hospital;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author raote
 * @since 2022-07-04
 */
public interface HospitalMapper extends BaseMapper<Hospital> {

    @Select("SELECT ch.checkdate as checkdate, com.doctornum AS doctornum FROM (SELECT re.id as id, re.hcode as hcode, re.doctornum as doctornum, hos.hospital FROM (SELECT * FROM t_report WHERE hcode = #{hcode}) AS re LEFT JOIN t_hospital as hos ON re.hcode  = hos.hcode ) AS com LEFT JOIN t_check AS ch on com.id = ch.rid where ch.status = 2 ORDER BY checkdate ")
    List<ChartDTO> getChart(String hcode);

    @Select("SELECT * FROM (SELECT re.id,  re.hcode, re.doctornum, re.address FROM t_report AS re LEFT JOIN t_check as ch ON re.id = ch.rid WHERE hcode = #{hcode} and status =2 ORDER BY checkdate DESC LIMIT 1) as com LEFT JOIN t_hospital as hos ON com.hcode = hos.hcode LIMIT 1")
    HospitalDTO checkOne(String hcode);

    @Select("SELECT hos.hcode, hos.hospital, hos.hrank, re.did FROM t_hospital as hos JOIN t_report AS re ON hos.hcode = re.hcode AND re.did = #{did}" +" ${ew.customSqlSegment}")
    Page<Hospital> hospitalPageByDep(@Param("page") Page<Hospital> page, @Param(Constants.WRAPPER) QueryWrapper<Hospital> qw, Integer did);
}
